﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;
using System.Data;

namespace BLL
{
    public class DiaDiemGiamGiaBLL
    {
        luuliDataContext DB = new luuliDataContext();
        //lay dd giam gia nhieu nhat
        public DataTable LayGiamGiaNhieuNhat()
        {
            DataTable a = LinqToDataTable(DB.diadiemgiamgias.OrderByDescending(q => q.Phamtramgiam_dd).Take(3));
            return a;

        }
        //lay dia diem giam gia moi nhat
        public DataTable layDDmoinhat()
        {
            DataTable Giamgianew = LinqToDataTable(DB.diadiemgiamgias.Where(q => q.Ngayketthuc_dd > DateTime.Now).OrderByDescending(q => q.Ngaytao_dd).Take(3));
            return Giamgianew;
        }
        //lay dia diem theo phan tram giam gia
        public DataTable LayDDGiamGia(int phantramgiamgia)
        {
            DataTable ddgiamgia = LinqToDataTable(DB.diadiemgiamgias.Where(p => (p.Phamtramgiam_dd >= phantramgiamgia)).OrderByDescending(q => q.Phamtramgiam_dd));
            return ddgiamgia;
        }
        public  DataTable laytatca()
        {
            DataTable a = LinqToDataTable(DB.sanphamgiamgias.Take(12).OrderByDescending(q => q.Id_sp));
            return a;
        }

        //tra ve datatalbe
        public DataTable LinqToDataTable<T>(System.Collections.Generic.IEnumerable<T> varlist)
        {
            try
            {
                System.Data.DataTable dtReturn = new System.Data.DataTable();
                System.Reflection.PropertyInfo[] oProps = null;
                if (varlist == null) return dtReturn;
                foreach (T rec in varlist)
                {
                    if (oProps == null)
                    {
                        oProps = ((Type)rec.GetType()).GetProperties();
                        foreach (System.Reflection.PropertyInfo pi in oProps)
                        {
                            Type colType = pi.PropertyType;
                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()== typeof(Nullable<>)))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                        dtReturn.Columns.Add(new System.Data.DataColumn(pi.Name, colType));
                        }
                    }
                    System.Data.DataRow dr = dtReturn.NewRow();
                    foreach (System.Reflection.PropertyInfo pi in oProps)
                    {
                        dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value :pi.GetValue(rec, null);
                    }
                    dtReturn.Rows.Add(dr);
                    }
                return dtReturn;
                }
                  catch (Exception ex) { return null; }
        }
        public DataTable selectdetail(int id)
        {
            DataTable a = LinqToDataTable(DB.diadiemgiamgias.Where(q => q.Id_diadiem == id));
            return a;
        }
        public String tencty_theoIDdd(int id)
        {
            DataTable a = LinqToDataTable(DB.diadiemgiamgias.Where(q => q.Id_diadiem == id));
            int ma_cty=int.Parse(a.Rows[0][8].ToString());
            DataTable cty = LinqToDataTable(DB.congties.Where(q => q.Id_cty == ma_cty));
            String ten_cty = cty.Rows[0][1].ToString();
            return ten_cty;

        }
    }
    
}
